/*
 * ErgSettingsWorkLoad.java
 *
 * Created on May 16, 2008, 11:54 AM
 */

package ca.jerg.app;

import java.awt.event.*;
import javax.swing.*;

import ca.jerg.config.*;

/**
 *
 * @author  dave
 */
public class ErgSettingsWorkLoad extends javax.swing.JFrame {
	private ErgConfigWork work;
	
	/**
	 * Get the value of work
	 *
	 * @return the value of work
	 */
	public ErgConfigWork getWork()
	{
		return work;
	}

	/**
	 * Set the value of work
	 *
	 * @param work new value of work
	 */
	public void setWork( ErgConfigWork work )
	{
		this.work = work;
	}
	
    /** Creates new form ErgSettingsWorkLoad */
    public ErgSettingsWorkLoad() {
        work = new ErgConfigWork();
		initComponents();
    }
	
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        anaerobic = new javax.swing.JSlider();
        vo2max = new javax.swing.JSlider();
        threshold = new javax.swing.JSlider();
        tempo = new javax.swing.JSlider();
        distance = new javax.swing.JSlider();
        recovery = new javax.swing.JSlider();
        anaerobicVal = new javax.swing.JTextField();
        vo2Val = new javax.swing.JTextField();
        thresVal = new javax.swing.JTextField();
        tempoVal = new javax.swing.JTextField();
        distVal = new javax.swing.JTextField();
        recovVal = new javax.swing.JTextField();
        jSeparator1 = new javax.swing.JSeparator();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("jErg - Work Load Settings");
        setResizable(false);

        jLabel1.setFont(new java.awt.Font("DejaVu Sans", 0, 15));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Adjust work loads based on precentage of threshold wattage");

        jLabel2.setFont(new java.awt.Font("DejaVu Sans", 1, 13));
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel2.setText("Work Zone");

        jLabel3.setFont(new java.awt.Font("DejaVu Sans", 1, 13));
        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel3.setText("Percentage");

        jLabel4.setText("Anaerobic");

        jLabel5.setText("VO2 Max");

        jLabel6.setText("Threshold");

        jLabel7.setText("Tempo");

        jLabel8.setText("Distance");

        jLabel9.setText("Recovery");

        anaerobic.setMaximum(200);
        anaerobic.setValue((int)(work.getDouble(IErgConfig.KEY_ANAEROBIC,ErgConfigWork.DEF_ANAEROBIC_LOAD) * 100));
        anaerobic.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                anaerobicStateChanged(evt);
            }
        });

        vo2max.setMaximum(200);
        vo2max.setValue((int)(work.getDouble(IErgConfig.KEY_VO2,ErgConfigWork.DEF_VO2_LOAD) * 100));
        vo2max.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                vo2maxStateChanged(evt);
            }
        });

        threshold.setMaximum(200);
        threshold.setValue((int)(work.getDouble(IErgConfig.KEY_FT,ErgConfigWork.DEF_FT_LOAD) * 100));
        threshold.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                thresholdStateChanged(evt);
            }
        });

        tempo.setMaximum(200);
        tempo.setValue((int)(work.getDouble(IErgConfig.KEY_TEMPO,ErgConfigWork.DEF_TEMPO_LOAD) * 100));
        tempo.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                tempoStateChanged(evt);
            }
        });

        distance.setMaximum(200);
        distance.setValue((int)(work.getDouble(IErgConfig.KEY_DISTANCE,ErgConfigWork.DEF_DISTANCE_LOAD) * 100));
        distance.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                distanceStateChanged(evt);
            }
        });

        recovery.setMaximum(200);
        recovery.setValue((int)(work.getDouble(IErgConfig.KEY_REST,ErgConfigWork.DEF_REST_LOAD) * 100));
        recovery.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                recoveryStateChanged(evt);
            }
        });

        anaerobicVal.setEditable(false);
        anaerobicVal.setText(String.valueOf(anaerobic.getValue() / 100.0));
        anaerobicVal.setMinimumSize(new java.awt.Dimension(35, 20));
        anaerobicVal.setPreferredSize(new java.awt.Dimension(77, 27));

        vo2Val.setEditable(false);
        vo2Val.setText(String.valueOf(vo2max.getValue() / 100.0));

        thresVal.setEditable(false);
        thresVal.setText(String.valueOf(threshold.getValue() / 100.0));

        tempoVal.setEditable(false);
        tempoVal.setText(String.valueOf(tempo.getValue() / 100.0));

        distVal.setEditable(false);
        distVal.setText(String.valueOf(distance.getValue() / 100.0));

        recovVal.setEditable(false);
        recovVal.setText(String.valueOf(recovery.getValue() / 100.0));

        jButton1.setText("Don't Save");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Save");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Defaults");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 566, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap(29, Short.MAX_VALUE)
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 218, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(217, 217, 217)
                        .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 102, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6)
                            .addComponent(jLabel7)
                            .addComponent(jLabel8)
                            .addComponent(jLabel9))
                        .addGap(41, 41, 41)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(recovery, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(distance, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(tempo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(threshold, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(vo2max, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(anaerobic, javax.swing.GroupLayout.DEFAULT_SIZE, 329, Short.MAX_VALUE))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(21, 21, 21)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(tempoVal, javax.swing.GroupLayout.DEFAULT_SIZE, 63, Short.MAX_VALUE)
                                    .addComponent(distVal, 0, 0, Short.MAX_VALUE)
                                    .addComponent(recovVal, 0, 0, Short.MAX_VALUE)))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(vo2Val, javax.swing.GroupLayout.Alignment.TRAILING, 0, 0, Short.MAX_VALUE)
                                .addComponent(anaerobicVal, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 63, Short.MAX_VALUE)
                                .addComponent(thresVal, javax.swing.GroupLayout.Alignment.TRAILING, 0, 0, Short.MAX_VALUE)))))
                .addContainerGap())
            .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 578, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 345, Short.MAX_VALUE)
                .addComponent(jButton3)
                .addGap(18, 18, 18)
                .addComponent(jButton2)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel4)
                    .addComponent(anaerobic, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(anaerobicVal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel5)
                    .addComponent(vo2max, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(vo2Val, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel6)
                    .addComponent(threshold, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(thresVal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel7)
                    .addComponent(tempo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tempoVal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel8)
                    .addComponent(distance, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(distVal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel9)
                    .addComponent(recovery, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(recovVal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2)
                    .addComponent(jButton3))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void anaerobicStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_anaerobicStateChanged
	anaerobicVal.setText(String.valueOf(anaerobic.getValue() / 100.0));
}//GEN-LAST:event_anaerobicStateChanged

private void vo2maxStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_vo2maxStateChanged
	vo2Val.setText(String.valueOf(vo2max.getValue() / 100.0));
}//GEN-LAST:event_vo2maxStateChanged

private void thresholdStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_thresholdStateChanged
	thresVal.setText(String.valueOf(threshold.getValue() / 100.0));
}//GEN-LAST:event_thresholdStateChanged

private void tempoStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_tempoStateChanged
	tempoVal.setText(String.valueOf(tempo.getValue() / 100.0));
}//GEN-LAST:event_tempoStateChanged

private void distanceStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_distanceStateChanged
	distVal.setText(String.valueOf(distance.getValue() / 100.0));
}//GEN-LAST:event_distanceStateChanged

private void recoveryStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_recoveryStateChanged
	recovVal.setText(String.valueOf(recovery.getValue() / 100.0));
}//GEN-LAST:event_recoveryStateChanged

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
	// the "Don't Save" button
	dispose();
}//GEN-LAST:event_jButton1ActionPerformed

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
	// the "Defaults" button
	work.defaults();
	initializeSliders();
}//GEN-LAST:event_jButton3ActionPerformed

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
	// the "Save" button
	work.putDouble(IErgConfig.KEY_ANAEROBIC,anaerobic.getValue() / 100.0);
	work.putDouble(IErgConfig.KEY_VO2,vo2max.getValue() / 100.0);
	work.putDouble(IErgConfig.KEY_FT,threshold.getValue() / 100.0);
	work.putDouble(IErgConfig.KEY_TEMPO,tempo.getValue() / 100.0);
	work.putDouble(IErgConfig.KEY_DISTANCE,distance.getValue() / 100.0);
	work.putDouble(IErgConfig.KEY_REST,recovery.getValue() / 100.0);
	
	dispose();
}//GEN-LAST:event_jButton2ActionPerformed

protected JRootPane createRootPane() 
{
	ActionListener actionListener = new ActionListener() 
	{
		public void actionPerformed(ActionEvent actionEvent) 
		{
			dispose();
		}
	};

	JRootPane rootPane = new JRootPane();
	KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE,0);
	rootPane.registerKeyboardAction(actionListener, stroke,JComponent.WHEN_IN_FOCUSED_WINDOW);

	return rootPane;
}
	
private void initializeSliders()
{
		anaerobicVal.setText(String.valueOf(work.getDouble(
				IErgConfig.KEY_ANAEROBIC,
				ErgConfigWork.DEF_ANAEROBIC_LOAD)));
		
		anaerobic.setValue((int)(work.getDouble(
				IErgConfig.KEY_ANAEROBIC,
				ErgConfigWork.DEF_ANAEROBIC_LOAD) * 100));

		vo2Val.setText(String.valueOf(work.getDouble(
				IErgConfig.KEY_VO2,
				ErgConfigWork.DEF_VO2_LOAD)));
		
		vo2max.setValue((int)(work.getDouble(
				IErgConfig.KEY_VO2,
				ErgConfigWork.DEF_VO2_LOAD) * 100));

		thresVal.setText(String.valueOf(work.getDouble(
				IErgConfig.KEY_FT,
				ErgConfigWork.DEF_FT_LOAD)));
		
		threshold.setValue((int)(work.getDouble(
				IErgConfig.KEY_FT,
				ErgConfigWork.DEF_FT_LOAD) * 100));
		
		tempoVal.setText(String.valueOf(work.getDouble(
				IErgConfig.KEY_TEMPO,
				ErgConfigWork.DEF_TEMPO_LOAD)));
		
		tempo.setValue((int)(work.getDouble(
				IErgConfig.KEY_TEMPO,
				ErgConfigWork.DEF_TEMPO_LOAD) * 100));
	
		distVal.setText(String.valueOf(work.getDouble(
				IErgConfig.KEY_DISTANCE,
				ErgConfigWork.DEF_DISTANCE_LOAD)));
		
		distance.setValue((int)(work.getDouble(
				IErgConfig.KEY_DISTANCE,
				ErgConfigWork.DEF_DISTANCE_LOAD) * 100));
		
		recovVal.setText(String.valueOf(work.getDouble(
				IErgConfig.KEY_REST,
				ErgConfigWork.DEF_REST_LOAD)));
		
		recovery.setValue((int)(work.getDouble(
				IErgConfig.KEY_REST,
				ErgConfigWork.DEF_REST_LOAD) * 100));
}

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ErgSettingsWorkLoad().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JSlider anaerobic;
    private javax.swing.JTextField anaerobicVal;
    private javax.swing.JTextField distVal;
    private javax.swing.JSlider distance;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField recovVal;
    private javax.swing.JSlider recovery;
    private javax.swing.JSlider tempo;
    private javax.swing.JTextField tempoVal;
    private javax.swing.JTextField thresVal;
    private javax.swing.JSlider threshold;
    private javax.swing.JTextField vo2Val;
    private javax.swing.JSlider vo2max;
    // End of variables declaration//GEN-END:variables

}
